#include "GameController.h"

GameController::GameController()
{
	city = new City( ) ;
	srand( unsigned( time( NULL ) ) ) ;
	//city->Display();

	m_State = NONE ;

	m_AcresPurchased = m_BushelsFed = m_AcresPlanted = 0 ;

	m_Lose = 0;
}

GameController::~GameController()
{

}

string GameController::RespondMessage( int input ) 
{
	m_State = input ;

	switch(input)
	{
	case PURCHASE:
		return ("\n\rHow many acres are you buying/selling? (Use negative numbers to sell acres you own): ") ;
		break ;
	case FEED:
		return ("\n\rHow many bushels will you feed to your people?: ") ;
		break ;
	case PLANT:
		return ("\n\rHow many acres will you plant for harvest?: ") ;
		break ;
	case SUBMIT:
		m_State = NONE ;
		Update() ;
		return "";
		break ;
	}

	
}

bool GameController::InputAmount( int amount )
{
	switch(m_State)
	{
	case PURCHASE:
		if( city->GetValidation( amount, PURCHASE ) )
		{
			m_AcresPurchased = amount ;
			m_State = NONE ;
			return true ;
		}
		else
			return false ;

		break ;
	case FEED:
		if( city->GetValidation( amount, FEED ) )
		{
			m_BushelsFed = amount ;
			m_State = NONE ;
			return true ;
		}
		else
			return false ;

		break ;
	case PLANT:
		if( city->GetValidation( amount, PLANT ) )
		{
			m_AcresPlanted = amount ;
			m_State = NONE ;
			return true ;
		}
		else
			return false ;

		break ;
	}
}

void GameController::Update( )
{
	int plagueChance, grainEaten, grainHarvested, immigrants, starved ;
	bool plague = false;
	//get the city's current population, remaining grain, and current acres owned
	int pop = city->GetPop() ;
	int grain = city->GetBushels() ;
	int acresOwned = city->GetAcres() ;

	//calculate the number of people fed for this year
	int peopleFed = m_BushelsFed / 20 ;

	//random value used for immigrant calculation
	int rValue = (rand() % 5) + 1 ;

	//get number of people born in the city
	immigrants = int(((rValue*(20*acresOwned + grain) / pop )/ 100) + 1) ;

	//calculate chance of plague
	plagueChance = (rand() % 100) - 15 ;

	//if a plague occurs, half the population dies
	if( plagueChance <= 0 )
		plague = true ;
	else
		plague = false;
	
	if( pop <= peopleFed )
		 starved = 0 ;
	else
	{
		starved = pop - peopleFed ;
		if( starved > (45*pop/100) )
		{
			m_Lose = true;	
		}
	}


	//get the amount of grain harvested based on the amount of acres planted
	grainHarvested = ((rand() % 5) + 1) ;

	//get a new random value for the chance of rats getting into the grain
	rValue = (rand() % 5) + 1 ;

	//if the rats get into the grain, grain is eaten by the random value
	if( rValue/2 != 2 || rValue/2 != 4 )
		grainEaten = grain / rValue ;

	//update the city with these new values
	city->UpdateCity( plague, immigrants, starved, grainEaten, grainHarvested, m_AcresPurchased, m_AcresPlanted ) ;

	m_AcresPurchased = m_BushelsFed = m_AcresPlanted = 0 ;
}

void GameController::UpdateBushels( int input )
{
	//checks with city for valid input
	//city->GetValidation( input );

	//refreshes the screen and re-displays text
	city->Display();
}

string GameController::EndGame( ) 
{ 
	if(m_Lose) 
		return city->DisplayLose();
	else 
		return city->DisplayEnd( );
}
